package server.views.servermngpanel;

import java.awt.BorderLayout;
import java.util.ArrayList;
//import java.util.HashMap;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;


import server.config.Public;
import server.doc.xml.XMLUserProfile;
import server.models.UserProfile;
import server.views.*;

/**
 * 在线用户表
 * 
 * @author solomon
 * 
 */

public class TablePanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private XMLUserProfile xmlProfile = XMLUserProfile.getInstance();
	
	private ArrayList<String> colArr = new ArrayList<String>();
	
	private MyTableModel dataModel = null;

	private JTable table = null;

	private JScrollPane scrollpane = null;

	public TablePanel(final ServerMain f) {
		colArr.add("用户ID");
		colArr.add("昵称");
		colArr.add("性别");
		colArr.add("登录时间");
		ArrayList<ArrayList<String>> profileList = new ArrayList<ArrayList<String>>();
		for (Integer uid : Public.status.getOnlineUsers()) {
			ArrayList<String> strProfile = new ArrayList<String>();
			UserProfile profile = xmlProfile.getUser(uid.intValue());
			strProfile.add(Integer.toString(profile.getUserID()));	// userID
			strProfile.add(profile.getName());						// nick name
			strProfile.add(profile.getGender().toString());			// gender
			strProfile.add(Public.dateFormat.format(Public.status.getOnlineTime(uid.intValue())));
																	// online time
			profileList.add(strProfile);
		}
		dataModel = new MyTableModel(profileList, colArr);
		table = new JTable(dataModel);
		scrollpane = new JScrollPane(table);
		this.setLayout(new BorderLayout());
		this.add(scrollpane);
		this.table.getSelectionModel().addListSelectionListener(
				new ListSelectionListener() {

					public void valueChanged(ListSelectionEvent e) {
						// 设置成可用的状态
						f.getServerMngPanel().getTopPanel().getLogOutBtn()
								.setEnabled(true);
					}

				});
	}

	public TablePanel() {
		colArr.add("用户ID");
		colArr.add("昵称");
		colArr.add("性别");
		colArr.add("登录时间");
		ArrayList<ArrayList<String>> profileList = new ArrayList<ArrayList<String>>();
		for (Integer uid : Public.status.getOnlineUsers()) {
			ArrayList<String> strProfile = new ArrayList<String>();
			UserProfile profile = xmlProfile.getUser(uid.intValue());
			strProfile.add(Integer.toString(profile.getUserID()));	// userID
			strProfile.add(profile.getName());						// nick name
			strProfile.add(profile.getGender().toString());			// gender
			strProfile.add(Public.dateFormat.format(Public.status.getOnlineTime(uid.intValue())));
																	// online time
			profileList.add(strProfile);
		}
		dataModel = new MyTableModel(profileList, colArr);		
		table = new JTable(dataModel);
		scrollpane = new JScrollPane(table);
		this.setLayout(new BorderLayout());
		this.add(scrollpane);
		this.table.getSelectionModel().addListSelectionListener(
				new ListSelectionListener() {

					public void valueChanged(ListSelectionEvent e) {

					}

				});
	}

	public JTable getTable() {
		return table;
	}

	public void setTable(JTable table) {
		this.table = table;
	}

	public MyTableModel getDataModel() {
		return dataModel;
	}

	public void setDataModel(MyTableModel dataModel) {
		this.dataModel = dataModel;
	}
}
